﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
using Protocol;

namespace MDS
{
    public class FutureQuotationProvider : IQuotationProvider
    {
        public event MessageHandler<Protocol.GWMKTBasicInfoANS> OnBasicMsgReceived;

        public event MessageHandler<Protocol.GWMKTBidAskANS> OnBidMsgReceived;

        public event MessageHandler<Protocol.GWMKTTicksANS> OnTickMsgReceived;

        public event MultiMessageHandler<Protocol.IMessage> MessagesReceived;


        protected FutureQuotationProviderConfig _config;
        protected SFITProxy _sfitProxy;
        public FutureQuotationProvider(FutureQuotationProviderConfig config, SFITProxy sfitProxy)
        {
            Contract.Requires(config != null);
            Contract.Requires(sfitProxy != null);
            this._config = config;
            this._sfitProxy = sfitProxy;
            this._sfitProxy.OnMessageReceived += new SFITMessageHandler(_sfitProxy_OnMessageReceived);
        }

        void _sfitProxy_OnMessageReceived(SFITRecord message)
        {
            //TODO:获取当前交易时间
            DateTime tradeTime = DateTime.Now;

            var msgs = buildMessages(message, tradeTime);
            if (msgs != null && msgs.Any())
                onMessagesReceived(msgs);
        }


        private List<Protocol.IMessage> buildMessages(
         SFITRecord record,
         DateTime tradeTime)
        {


            if (record.Exch == FutureExchange.JinRong && record.StkId.Contains(FutureType.TF))
                return null;
            List<Protocol.IMessage> rtn = new List<IMessage>();
            GWMKTBasicInfoANS basicInfo = buildBasicInfo(record, tradeTime);
            rtn.Add(basicInfo);

            GWMKTTicksANS tick = buildTick(record, tradeTime);
            if (tick != null)
                rtn.Add(tick);
            GWMKTBidAskANS bid = buildBid(record, tradeTime);
            rtn.Add(bid);





            return rtn;
        }

        private GWMKTBidAskANS buildBid(SFITRecord record, DateTime tradeTime)
        {
            throw new NotImplementedException();
        }

        private GWMKTTicksANS buildTick(SFITRecord record, DateTime tradeTime)
        {
            throw new NotImplementedException();
        }

        private GWMKTBasicInfoANS buildBasicInfo(SFITRecord record, DateTime tradeTime)
        {
            throw new NotImplementedException();
        }
        protected void onMessagesReceived(IEnumerable<IMessage> messages)
        {
            MultiMessageHandler<IMessage> handler = this.MessagesReceived;
            if (handler != null)
                handler(messages);
        }

        public void Start()
        {
            throw new NotImplementedException();
        }

        public void Stop()
        {
            throw new NotImplementedException();
        }

        public void Clear()
        {
            throw new NotImplementedException();
        }
    }
}
